#include "prosetpage.h"
#include "ui_prosetpage.h"
#include<QDir>
#include<QFileDialog>

ProSetPage::ProSetPage(QWidget *parent) :
    QWizardPage(parent),
    ui(new Ui::ProSetPage)
{
    ui->setupUi(this);
    //    注册域,将字段内容注册，使得在本类中随时可以使用 filed() 和 setFiled() 访问
    registerField("proPath", ui->lineEdit_2);
    //    加上 * ，则当前页面该字段必填，否则无法点击下一步
    registerField("proName*", ui->lineEdit);

    // 当文本输入后检测当前界面的限制条件是否完成--->是调用的isComplete去判断
    connect(ui->lineEdit, &QLineEdit::textEdited, this, &ProSetPage::completeChanged);
    connect(ui->lineEdit_2, &QLineEdit::textEdited, this, &ProSetPage::completeChanged);

    QString curPath = QDir::currentPath();
    ui->lineEdit_2->setText(curPath);
//    设置光标进入编辑器时的默认位置
    ui->lineEdit_2->setCursorPosition(ui->lineEdit_2->text().size());
//    给编辑框添加 清空 按钮
    ui->lineEdit_2->setClearButtonEnabled(true);
    ui->lineEdit->setClearButtonEnabled(true);
}

ProSetPage::~ProSetPage()
{
    delete ui;
}

void ProSetPage::GetProSettings(QString &name, QString &path)
{
    name = ui->lineEdit->text();
    path = ui->lineEdit_2->text();
}

bool ProSetPage::isComplete() const
{
    if(ui->lineEdit->text().isEmpty() || ui->lineEdit_2->text().isEmpty()) {
        return false;
    }

//    判断路径是否存在
    QDir dir(ui->lineEdit_2->text());
    if(!dir.exists())
    {
        ui->tips->setText("project path is not exists");
        return false;
    }

//    判断项目是否存在
    QString absFilePath = dir.absoluteFilePath(ui->lineEdit->text());
    QDir dist_dir(absFilePath);
    if(dist_dir.exists())
    {
        ui->tips->setText("project has exists, change path or name");
        return false;
    }
    ui->tips->setText("");
    //    自定义的限制通过了，接下来检测默认的限制条件
    return QWizardPage::isComplete();
}

void ProSetPage::on_pushButton_clicked()
{
    QFileDialog file_dialog;
    file_dialog.setFileMode(QFileDialog::Directory);
    file_dialog.setWindowTitle("选择导入的文件夹");
    auto path = QDir::currentPath();
    file_dialog.setDirectory(path);
    file_dialog.setViewMode(QFileDialog::Detail);

    QStringList fileNames;
    if(file_dialog.exec())
    {
        fileNames = file_dialog.selectedFiles();
    }

    if(fileNames.length() <= 0)
    {
        return;
    }

    QString import_path = fileNames.at(0);

    ui->lineEdit_2->setText(import_path);
}
